<template>
  <div class="update-photo">
    <img :src="img" alt="" ref="img" class="img" />

    <div class="toolbar">
      <div class="cancel" @click="$emit('close')">取消</div>
      <div class="confirm" @click="onConfirm">完成</div>
    </div>
  </div>
</template>

<script>
import "cropperjs/dist/cropper.css";
import Cropper from "cropperjs";
import { updateUserPhoto } from "@/api/user.js";
export default {
  name: "UpdatePhoto",
  data() {
    return {
      cropper: null,
    };
  },
  props: {
    img: {
      type: [String, Object],
      required: true,
    },
  },
  created() {},
  mounted() {
    const image = this.$refs.img; //获取元素;
    this.cropper = new Cropper(image, {
      // aspectRatio: 16 / 9, //默认比例
      viewMode: 1,
      dragMode: "move",
      aspectRatio: 1,
      // autoCropArea: 1,
      cropBoxMovable: false,
      cropBoxResizable: false,
      background: false,
      //  movable: true
      // crop(event) {
      // viewMode: 1, // 查看模式 0  1
      // dragMode: 'crop'// 拖动模式; crop  move none
      // aspectRatio: 1, // 截取的区域1:1
      // autoCropArea: 1,
      // cropBoxMovable: false, // 是否可以移动 高亮区域是否可移动
      // cropBoxResizable: false,
      // background: false, //关掉背景
      // movable: true //配置画布是否可以移动
      // console.log(event.detail.x);
      // console.log(event.detail.y);
      // console.log(event.detail.width);
      // console.log(event.detail.height);
      // console.log(event.detail.rotate);
      // console.log(event.detail.scaleX);
      // console.log(event.detail.scaleY);
      // },
    });
  },
  methods: {
    onConfirm() {
      this.$toast.loading({
        message: "头像更新中...",
        forbidClick: true,
        duration: 0,
      });

      // this.cropper.getData();
      this.cropper.getCroppedCanvas().toBlob((blob) => {
        // updateUserPhoto({
        //   photo:blod
        // })
        this.updatePhoto(blob);
      });
    },
    async updatePhoto(blob) {
      try {
        const formData = new FormData();
        formData.append("photo", blob);

        const {
          data: {
            data: { photo },
          },
        } = await updateUserPhoto(formData);
        console.log(photo, blob);
        // 关闭弹出层
        this.$emit("close");
        // 更新头像
        this.$emit("update-photo", photo);
        this.$toast.success("头像更新成功!")
      } catch (err) {
        this.$toast.fail("头像更新失败!");
      }
    },
  },
};
</script>

<style scoped lang="less">
.update-photo {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #000;
  .img {
    max-width: 100%;
    display: block;
  }
  .toolbar {
    position: fixed;
    bottom: 0px;
    left: 0;
    right: 0;
    //   height: 50px;
    display: flex;
    justify-content: space-between;
    //   padding: 0 15px;
    font-size: 30px;
    //   font-weight: bold;
    .cancel,
    .confirm {
      width: 90px;
      height: 90px;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
    }
  }
}
</style>
